<?php

declare(strict_types=1);
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/10/31
 * Time: 3:48 PM
 */

namespace App\Entity;

use App\Models\Area;
use Illuminate\Support\Str;

class AreaEntity extends ABaseEntity
{
    public function __construct(Area $model)
    {
        $this->model = $model;
    }

    /**
     * @return AreaEntity|null
     */
    public static function getEntityByAreaName(string $areaName)
    {
        $areaName = Str::finish($areaName, '市');
        $model    = Area::where('areaname', $areaName)->first();
        if ($model === null) {
            return null;
        }

        return new self($model);
    }

    /**
     * @return string
     */
    public static function getAreaNameById(int $channelId)
    {
        $entity = self::find($channelId);

        return $entity === null ? '' : $entity->getAreaname();
    }

    /**
     * @return int
     */
    public static function getCityIdByAreaName(string $areaName)
    {
        $areaEntity = self::getEntityByAreaName($areaName);
        if ($areaEntity === null) {
            return 0;
        }

        return $areaEntity->getId();
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->model->id;
    }

    /**
     * @return string
     */
    public function getAreaname()
    {
        return $this->model->areaname;
    }

    /**
     * @return int
     */
    public function getArealevel()
    {
        return $this->model->level;
    }

    /**
     * @return int
     */
    public function getAreaparentid()
    {
        return $this->model->parentid;
    }

    /**
     * @return int
     */
    public function getAreaisopen()
    {
        return $this->model->is_open;
    }
}
